package com.example.mvvmdemo.taobao

import android.graphics.Rect
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.mvvmdemo.R
import com.example.mvvmdemo.base.LoadState
import com.example.mvvmdemo.utils.SizeUtils
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import kotlinx.android.synthetic.main.activity_on_error.*
import kotlinx.android.synthetic.main.activity_on_sell.*

/**
 * @author Liyg
 * @description: 特惠页面
 * @date :2020/11/3
 */
class OnSellActivity:AppCompatActivity(){

    //获取viewModel,通过懒加载的方式
    private val mViewModel by lazy {
        ViewModelProvider(this).get(OnSellViewModel::class.java)
    }

    //创建适配器
    private val mAdapter by lazy {
        OnSellListAdapter()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_on_sell)
        initView()
        initObserver()
        initListener()
    }

    private fun initListener() {
        //重新加载
        reloadLL.setOnClickListener(){
            mViewModel.loadContent()
        }
    }

    /**
     * 观察数据变化
     */
    private fun initObserver() {
        mViewModel.apply {
            //加载数据完成后对页面进行更新
            contentList.observe(this@OnSellActivity, Observer {
                //内容列表更新
                //更新适配器
                mAdapter.setData(it)
            })

            //页面加载的状态完成后对页面进行更新
            loadState.observe(this@OnSellActivity, Observer {
                hideAll()
                when(it){
                    LoadState.LOADING ->{
                        loadingView.visibility = View.VISIBLE
                    }
                    LoadState.EMPTY ->{
                        emptyView.visibility = View.VISIBLE
                    }
                    LoadState.ERROR ->{
                        errorView.visibility = View.VISIBLE
                    }
                    else -> {
                        contentRefreshView.visibility = View.VISIBLE
                        contentRefreshView.finishLoadmore()
                    }
                }
                when(it){
                    LoadState.LOAD_MORE_ERROR -> {
                        Toast.makeText(this@OnSellActivity,"网络不佳，请重新加载更多内容...",Toast.LENGTH_LONG).show()
                    }
                    LoadState.LOAD_MORE_EMPTY -> {
                        Toast.makeText(this@OnSellActivity,"暂无更多内容...",Toast.LENGTH_LONG).show()
                    }
                }
            })

        }.loadContent()


    }

    /**
     * 初始化View
     */
    private fun initView() {

        contentRefreshView.run {
            setEnableLoadmore(true)
            setEnableRefresh(false)
            setEnableOverScroll(true)
            setOnRefreshListener(object : RefreshListenerAdapter(){
                override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                    //去执行加载更多
                    mViewModel.loaderMore()
                }
            })
        }

        contentListRv.run {
            //设置RecyclerView的LayoutManager
            layoutManager = LinearLayoutManager(this@OnSellActivity)
            //设置Adapter
            adapter = mAdapter
            //添加分割线
            addItemDecoration(
                object :RecyclerView.ItemDecoration(){
                    override fun getItemOffsets(
                        outRect: Rect,
                        view: View,
                        parent: RecyclerView,
                        state: RecyclerView.State
                    ) {
                        outRect.apply {
                            val padding = SizeUtils.dip2px(this@OnSellActivity,8.0f)
                            top = padding/2
                            bottom = padding/2
                            left = padding
                            right = padding
                        }
                    }
                }
            )
        }
    }


    private fun hideAll(){
        contentRefreshView.visibility = View.GONE
        loadingView.visibility = View.GONE
        emptyView.visibility = View.GONE
        errorView.visibility = View.GONE
    }
}